package com.fbt.andon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.Line;
import com.fbt.andon.service.impl.LineServiceImpl;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 产线Controller
 *
 * @author fengtao
 * @date 2021-08-27
 */
@RestController
@RequestMapping("/line")
@Api(description = "产线api")
public class LineController extends BaseController {
    @Autowired
    private LineServiceImpl lineService;


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @GetMapping("/query")
    @ApiOperation("查询所有产线列表")
    public AjaxResult query() {
        startPage();
        return AjaxResult.success("查询成功", lineService.list(null));
    }


    /**
     * 导出产线列表
     */
    //@PreAuthorize(hasPermi = "mes:andon:export")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Line line) throws IOException {
        List<Line> list = lineService.list(null);
        ExcelUtil<Line> util = new ExcelUtil<Line>(Line.class);
       // util.exportExcel(response, list, "line");
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/findByQueryParams")
    @ApiOperation("根据条件模糊查询产线信息")
    public AjaxResult findByQueryParams(@RequestBody Line line) {
        startPage();
        if (line.getLineName() != null && line.getPrincipal() != null) {
            QueryWrapper<Line> wrapper = new QueryWrapper<>();
            wrapper.like("line_name", line.getLineName());
            wrapper.like("principal", line.getPrincipal());
            return AjaxResult.success("查询成功", lineService.list(wrapper));
        } else if (line.getLineName() != null) {
            QueryWrapper<Line> wrapper1 = new QueryWrapper<>();
            wrapper1.like("line_name", line.getLineName());
            return AjaxResult.success("查询成功", lineService.list(wrapper1));
        } else if (line.getPrincipal() != null) {
            QueryWrapper<Line> wrapper2 = new QueryWrapper<>();
            wrapper2.like("principal", line.getPrincipal());
            return AjaxResult.success("查询成功", lineService.list(wrapper2));
        }
        return AjaxResult.success("查询成功", null);
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取产线详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", lineService.getById(id));
    }


    //    //@PreAuthorize(hasPermi = "andon:line:add")
    @PostMapping("/add")
    @ApiOperation("新增产线")
    public AjaxResult add(@RequestBody Line line) {
        return AjaxResult.success(lineService.save(line));
    }

    //    //@PreAuthorize(hasPermi = "andon:worker:query")
    @ApiOperation("根据id查询人员")
    @GetMapping(value = "/{id}")
    public AjaxResult getByid(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", lineService.getById(id));
    }


    //    //@PreAuthorize(hasPermi = "andon:line:edit")
    @PostMapping("/update")
    @ApiOperation("修改产线")
    public AjaxResult edit(@RequestBody Line line) {
        return AjaxResult.success("修改成功", lineService.updateById(line));
    }


    //@PreAuthorize(hasPermi = "mes:andon:remove")
    @PostMapping("/batchRemove/{ids}")
    @ApiOperation("删除产线")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return AjaxResult.success("删除成功", lineService.removeByIds(Arrays.asList(ids)));
    }
}
